package com.rd.student.jike.class2.lihongming.week731_806.Day0801;

import java.util.Arrays;

/**
 * @Auther:Heinrich
 * @Date:2023/8/1-08-01-11:37
 * @Description:com.rd.student.jike.class2.lihongming.Day0801
 * @version:1.0
 */
public class ArrayHandle {
    public static void main(String[] args) {
        int[] arr = {1,3,5,7,9,11,13,15,17,0};
        arrayIns(arr,5,10);
        System.out.println(Arrays.toString(arr));
        int[] arr1 = {1,3,5,7,9,11,13,15,17};
        arrayDel(arr1,5);
        //判断是否为增序
        System.out.println(arrayIncOrderly(arr1));
        System.out.println(arrayIncOrderly((arr)));
        //两有序数组合并
        int[] arr2 = {2,4,6,8,10,12,14,16,18,21};
        int[] arrMerge = arrayOrderlyMerge(arr,arr2);
        System.out.println(Arrays.toString(arrMerge));
        //
        System.out.println(toString(arrMerge));
        BubbleSort(arrMerge);
        //System.out.println(toString(arrMerge));
        int[] arrP = {60,77,33,55,11,22,80};
        //BubbleSort(arrP);
        SelectionSort(arrP);
        System.out.println(toString(arrP));
    }

    public static void arrayIns(int[] arr,int index,int number) {
        System.arraycopy(arr,index,arr,index+1,arr.length-index-1);
        arr[index] = number;
    }

    public static void arrayDel(int[] arr, int index) {
        //传入值而非下标的解法
//        int x;
//        for (int i = 0; i < arr.length; i++) {
//            if(arr[i]==index){
//                x=i;
//                break;
//            }
//        }
        System.arraycopy(arr,index+1,arr,index,arr.length-index-1);
        arr[arr.length-1] = 0;
    }
    public static boolean arrayIncOrderly(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            if(array[i]>array[i+1]){
                return false;
            }
        }
        return true;
    }

    public static int[] arrayOrderlyMerge(int[] arr1,int[] arr2){
        int[] arr = new int[arr1.length + arr2.length];
        int j = 0, k = 0,i=0;
        while (i < arr.length) {
            arr[i++] = arr1[j] > arr2[k] ? arr2[k++] : arr1[j++];
            if(j>=arr1.length){
                while (k<arr2.length){
                    arr[i++] = arr2[k++];
                }
            }
            if(k>=arr2.length){
                while (j<arr1.length){
                    arr[i++] = arr1[j++];
                }
            }
        }
        return arr;
    }

    public static String toString(int[] arr){
        StringBuilder str = new StringBuilder("[");
        for (int i : arr) {
            str.append(i);
            str.append(',');
        }
        str.replace(str.length()-1,str.length(),"]");
        return new String(str);
    }

    //从大到小
    public static void BubbleSort(int[] arr) {
        if(arr==null||arr.length<2) return;
        int exchange = arr.length;
        int len = arr.length - 1;
        while (exchange >= 1) {
            exchange = 0;
            for (int i = 0; i < len; i++) {
                if (arr[i] < arr[i + 1]) {
                    arr[i] = arr[i] ^ arr[i + 1];
                    arr[i + 1] = arr[i] ^ arr[i + 1];
                    arr[i] = arr[i] ^ arr[i + 1];
                    exchange = i;
                }
            }
            len = exchange;
        }
    }
    //选择排序
    public static void SelectionSort(int[] arr) {
        if(arr==null||arr.length<2) return;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if(arr[i]>arr[j]){
                    arr[i] = arr[i] ^ arr[j];
                    arr[j] = arr[i] ^ arr[j];
                    arr[i] = arr[i] ^ arr[j];
                }
            }
        }
    }
}
